//
//  XiuGaiPhoneViewController.m
//  lmq
//
//  Created by 马田成 on 2017/4/19.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "XiuGaiPhoneViewController.h"

@interface XiuGaiPhoneViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneaaaa;
@property (weak, nonatomic) IBOutlet UITextField *yanzgengma;
@property (weak, nonatomic) IBOutlet UIButton *buttonYZ;
@property (weak, nonatomic) IBOutlet UIButton *queren;

@end

@implementation XiuGaiPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"修改手机号";
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)yanzhengButton:(UIButton *)sender {
    
    
    __block int timeout=60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    NSMutableDictionary *loginParams=[[NSMutableDictionary alloc]init];
    [loginParams setValue:self.phoneaaaa.text forKey:@"phone"];
    
    [[MNetworkHandler sharedInstance]conURL:Send_URL
                                networkType:NetWorkPOST
                                     params:loginParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   
                                   [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   
                               }
                               failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
    dispatch_source_set_event_handler(_timer, ^{
        
        if(timeout<=0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.buttonYZ setTitle:@"发送验证码" forState:UIControlStateNormal];
                
                self.buttonYZ.userInteractionEnabled = YES;
            });
        }else{
            int seconds = timeout % 61;
            
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                //NSLog(@"____%@",strTime);
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                [self.buttonYZ setTitle:[NSString stringWithFormat:@"%@秒",strTime] forState:UIControlStateNormal];
                [UIView commitAnimations];
                self.buttonYZ.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);

    
    
}
- (IBAction)querenButton:(UIButton *)sender {
    
    NSMutableDictionary *loginParams=[[NSMutableDictionary alloc]init];
    [loginParams setValue:self.phoneaaaa.text forKey:@"phone"];
    [loginParams setValue:self.yanzgengma.text forKey:@"code"];
    NSString *tokenStr=[PublicFunction getConfigWithKey:@"token"];
   
    [loginParams setValue:tokenStr forKey:@"token"];
    [[MNetworkHandler sharedInstance]conURL:My_modifyPhone
                                networkType:NetWorkPOST
                                     params:loginParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       [self.navigationController popViewControllerAnimated:YES];
                                       
                                       
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }else {
                                       NSLog(@"%@", loginParams);
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                       
                                   }
                                   
                                   
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
    

    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
